package top.lingkang.screen;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.utils.viewport.FitViewport;
import top.lingkang.CoolRunGame;
import top.lingkang.listener.WorldListener;
import top.lingkang.sprites.BackgroundSprite;
import top.lingkang.sprites.GroundSprite;
import top.lingkang.sprites.PlayerSprite;

public class PlayScreen extends GameScreen {
    private CoolRunGame game;
    public SpriteBatch batch;
    private Stage stage;
    public OrthographicCamera camera;

    private World world;
    private Box2DDebugRenderer debugRenderer;

    public float moveSpeed = 4f;

    public PlayerSprite playerSprite;
    public GroundSprite groundSprite;
    private BackgroundSprite backgroundSprite;
    private Label juli;

    public PlayScreen(CoolRunGame game) {
        this.game = game;
        this.camera = game.camera;
        batch = game.batch;
        camera.setToOrtho(false, Gdx.graphics.getWidth() / 64, Gdx.graphics.getHeight() / 64);
        stage = new Stage(new FitViewport(CoolRunGame.VIEW_WIDTH, CoolRunGame.VIEW_HEIGHT, new OrthographicCamera()), batch);

        // 创建一个世界，里面的重力加速度为 10
        world = new World(new Vector2(0, -20), true);
        // 试调渲染，可以使用这个渲染观察到我们用Box2D绘制的物体图形
        debugRenderer = new Box2DDebugRenderer();

        // 创建玩家
        playerSprite = new PlayerSprite(this, world, 0, 400);
        groundSprite = new GroundSprite(this, world);
        backgroundSprite = new BackgroundSprite(this);

        // 碰撞监听
        world.setContactListener(new WorldListener(this));

        // libgdx 默认的样式是不支持中文的，要自己去制作样式的，可以参考前面的文章
        Label.LabelStyle labelStyle = new Label.LabelStyle(game.bitmapFont, Color.WHITE);
        Table table = new Table();
        table.top();
        table.setFillParent(true);
        table.add(new Label("fenshu", labelStyle)).expandX().padTop(10);
        table.add(new Label("juli", labelStyle)).expandX().padTop(10);
        table.row();// 添加新行
        table.add(new Label("666", labelStyle)).expandX();
        // 用于更新这个标签的效果
        juli = new Label("0", labelStyle);
        table.add(juli).expandX();
        stage.addActor(table);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);


        // 将绘制与相机投影绑定 关键 关键
        batch.setProjectionMatrix(camera.combined);
        batch.begin();

        // 地面更新
        groundSprite.update(delta);

        // 背景更新
        backgroundSprite.update(delta);

        // 渲染玩家
        playerSprite.update(delta);

        batch.end();

        // 渲染物理世界轮廓
        debugRenderer.render(world, camera.combined);


        camera.position.x = playerSprite.position.x;
        camera.update();

        // 距离
        juli.setText((int) (playerSprite.position.x * 2));
        stage.draw();

        // 更新物理世界
        world.step(delta, 2, 1);
    }

    @Override
    public void dispose() {
        super.dispose();
        world.dispose();
        stage.dispose();
        batch.dispose();
    }
}
